package cn.melonlib.nars.record.comp.service;

import cn.melonlib.cores.aop.anno.inject.FilterRecord;
import cn.melonlib.cores.aop.anno.inject.FilterTenant;
import cn.melonlib.cores.aop.anno.inject.InjectRecord;
import cn.melonlib.cores.aop.anno.inject.InjectTenant;
import cn.melonlib.cores.aop.comp.web.WebInfo;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.identification.comp.messenger.MessengerUtils;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.jpa.model.bean.impl.OffsetQueryImpl;
import cn.melonlib.cores.jpa.model.bean.impl.PaginationList;
import cn.melonlib.cores.model.anno.WorkerComment;
import cn.melonlib.cores.model.anno.WorkerModel;
import cn.melonlib.cores.model.anno.WorkerParameter;
import cn.melonlib.cores.model.anno.WorkerService;
import cn.melonlib.nars.names.comp.service.NamesService;
import cn.melonlib.nars.names.model.entity.MelonNames;
import cn.melonlib.nars.record.model.entity.NamesVisitorRecord;
import cn.melonlib.nars.record.model.entity.NamesVisitorRecord;
import cn.melonlib.nars.record.model.enums.OperatorType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.*;
import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.SAVE;

@WorkerComment("资源访问日志")
@WorkerService(
        module = "records"
        ,model = @WorkerModel(
                modelType = NamesVisitorRecord.class
)
        ,ignores = {SAVE,SEARCH,REMOVE_BY_ID_LIST,REMOVE_BY_ID,LIST,LOAD_ALL,TOTAL_COUNT,SAVE}
)
@Service
@InjectRecord
@InjectTenant
@FilterRecord
@FilterTenant
public class NamesVisitorRecordService extends WorkerBasicService<NamesVisitorRecord> {

    @Resource
    private NamesService namesService;

    @Resource
    private MessengerUtils messengerUtils;

    @Resource
    private WebInfo webInfo;

    public NamesVisitorRecord addRecord(
            @WorkerParameter(value = "namesId",comment = "资源ID",required = true) String namesId,
            @WorkerParameter(value = "operatorType",comment = "操作类型",required = true) OperatorType operatorType
    )throws Exception{
        MelonNames names=namesService.getById(namesId);
        NamesVisitorRecord namesVisitorRecord=new NamesVisitorRecord();
        messengerUtils.setMessengers(namesVisitorRecord);
        namesVisitorRecord.setNamesId(names.getId());
        namesVisitorRecord.setIp(webInfo.getClientInfo().getIp());
        namesVisitorRecord.setBody(getJsonMapper().writeValueAsString(webInfo.getClientInfo()));
        return dao.save(namesVisitorRecord).getResult();
    }

    public PaginationList<NamesVisitorRecord> listRecord(
            @WorkerParameter(value = "path",comment = "资源路径",required = true) String path,
            @WorkerParameter(value = "first",comment = "数量",required = true) Integer first,
            @WorkerParameter(value = "offset",comment = "起始",required = true) Long offset
    ){
        MelonNames names=namesService.lookup(path);
        return this.dao.<PaginationList<NamesVisitorRecord>, OffsetQueryImpl>search(new OffsetQueryImpl(NamesVisitorRecord.class,
                JsonMap.<JsonMap<Object>>createMap().append("path",JsonMap.createMap().append("eq",names.getPath()))
                ,offset,first
        )).getResult();
    }
}
